package top.leyi.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.IClassService;
import top.leyi.IService.IUserService;
import top.leyi.entity.Class;
import top.leyi.entity.user.User;
import top.leyi.exception.ClassNotSetException;
import top.leyi.exception.NoSuchClassException;
import top.leyi.exception.NotSuchUserIdException;
import top.leyi.exception.TokenException;
import top.leyi.utils.Result;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/api/user")
@RestController
public class UserHandle {

    /**
     * 获取个人信息
     */
    @Autowired
    IUserService userService;
    @Autowired
    IClassService classService;

    @GetMapping("/search")
    public Result getOtherUserData(@RequestParam("token") String token,
                                   @RequestParam("id") int id)
                                   throws NotSuchUserIdException, TokenException {
        User user = new User();
        user.setId(id);
        return new Result(200, userService.getOtherPersonInfo(token, user));
    }


    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result getUserData(String token)throws TokenException {
        return new Result(200, userService.getPersonInfo(token));
    }


    /**
     * 修改个人信息
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public Result<Map>  updataUser (@PathVariable("id") String id,
                                    @RequestParam("token") String token,
                                    @RequestParam(value = "headPic", required = false) String headPic,
                                    @RequestParam(value = "wechat", required = false) String  wechat,
                                    @RequestParam(value = "username", required = false) String  username,
                                    @RequestParam(value = "qq", required = false) String  qq,
                                    @RequestParam(value = "classId",  required = false) Integer  classId,
                                    @RequestParam(value = "phone",  required = false) String  phone,
                                    @RequestParam(value = "email",  required = false) String  email,
                                    @RequestParam(value = "bgpic",  required = false) String  bgPic,
                                    @RequestParam(value = "word",  required = false) String  word,
                                    @RequestParam(value = "study_time",  required = false) String studyTime,
                                    @RequestParam(value = "college",  required = false) String college,
                                    @RequestParam(value = "roleId",  required = false) Integer roleId
                                                    ) throws  NoSuchClassException, TokenException, ClassNotSetException {

        Map<String, ? super  Object> map  = new HashMap<>();

        if(classId != null  ){
            Class clazz = new Class( classId);
            classId = classService.queryClass(clazz);
            if(classId == null){
                throw new NoSuchClassException();
            }
        }
        else {
            classId = 0;
        }
        User user =  new User(
                classId, username,  wechat,  headPic,  qq,   phone,  email, word, bgPic, studyTime, college);
        if(roleId != null) {
            user.setRoleId(roleId);
        }
        return new Result(200, userService.updataPersonInfo(token ,user ));
    }


}
